package chapter04.sync;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class ABCTest3 {
    public static void main(String[] args) throws InterruptedException {
        AwaitSingal awaitSingal = new AwaitSingal(5);
        Condition a = awaitSingal.newCondition();
        Condition b = awaitSingal.newCondition();
        Condition c = awaitSingal.newCondition();
        new Thread(()->{awaitSingal.print("a",a,b);},"t1").start();
        new Thread(()->{awaitSingal.print("b",b,c);},"t2").start();
        new Thread(()->{awaitSingal.print("c",c,a);},"t3").start();
        awaitSingal.lock();
        a.signal();
        awaitSingal.unlock();
    }
}
class AwaitSingal extends ReentrantLock{
    private int loopnumber;


    public AwaitSingal(int loopnumber) {
        this.loopnumber = loopnumber;
    }

    public void print(String str, Condition current,Condition next){
        for(int i=0;i<loopnumber;i++){
            lock();
            try {
                try {
                    current.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.printf(str);
                next.signal();
            }finally {
                unlock();
            }
        }
    }


}
